package com.example.star.controller;


import com.example.star.entity.Feedback;
import com.example.star.entity.Reply;
import com.example.star.entity.User;
import com.example.star.service.ReplyService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @PostMapping("/reply")
    @ResponseBody
    public void createReply(@RequestBody Reply reply) {
        replyService.createReply(reply);
    }

    @GetMapping("/reply")
    @ResponseBody
    public List<Reply> getAllReply() {
        return replyService.getAllReply();
    }

    @GetMapping("/reply/user/{userName}")
    @ResponseBody
    public List<Reply> getRepliesByUserName(@PathVariable String userName) {
        return replyService.getRepliesByUserName(userName);
    }

    @GetMapping("/reply-list")
    public String showReplyList(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "8") int size,
                                Model model,
                                HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            // 用户未登录，重定向到登录页面
            return "redirect:/login";
        }
        User user = (User) userObj;
        List<Reply> replies;
        int totalReplies;
        if ("ADMIN".equals(user.getRole())) {
            // 管理员角色，获取所有回复信息
            replies = replyService.getReplyByPage(page, size);
            totalReplies = replyService.getTotalReply();
        } else {
            // 普通用户角色，获取该用户的回复信息
            replies = replyService.getRepliesByUserNameAndPage(user.getUsername(), page, size);
            totalReplies = replyService.getTotalRepliesByUserName(user.getUsername());
        }
        int totalPages = (int) Math.ceil((double) totalReplies / size);

        model.addAttribute("reply", replies);
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", totalPages);

        return "reply-list";
    }

    @PostMapping("/reply/{id}")
    public String deleteFeedBack(@PathVariable int id, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        // 从 HttpSession 中获取用户信息
        HttpSession session = request.getSession();
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            redirectAttributes.addFlashAttribute("error", "你还未登录，请先登录");
            return "redirect:/login"; // 重定向到登录页面
        }

        User user = (User) userObj;
        // 假设管理员角色名称为 "ADMIN"，根据实际情况修改
        if ("ADMIN".equals(user.getRole())) {
            try {
                replyService.deleteReplyById(id);
                redirectAttributes.addFlashAttribute("message", "回复信息删除成功");
            } catch (Exception e) {
                redirectAttributes.addFlashAttribute("error", "删除回复信息时出现错误：" + e.getMessage());
            }
        } else {
            redirectAttributes.addFlashAttribute("error", "你没有权限删除回复信息");
        }
        return "redirect:/reply-list";
    }

}

